<?php

// $Id$

require_once(TANGRA_MAIN_DIR.'db/paginate_db_data.class.php');
require_once(TANGRA_MAIN_DIR.'grids/grid_ctrl.class.php');

require_once('ci_attachment_select_grid.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/tcms_ci_attachment_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/attachment/tcms_attachment_dbc.class.php');


class CI_Attachment_Select_Grid_Ctrl extends Grid_Ctrl {
	private $ci_id;

	function __construct($system_name, Vars_Manager $vm, $method = Tangra_Parameter_Method::GET, $ci_id) {
		tangra_if_not_int_throw_e($ci_id);
		$this->ci_id = $ci_id;

		parent::__construct($system_name, $vm, $method);
	}


	protected function create_grid() {
		$ret = new CI_Attachment_Select_Grid();


		return $ret;
	}


	protected function create_paginate_db_data() {
		$sqls = '';
		$sqlc = '';

		$pdbd = new Paginate_DB_Data($this->grid->get_page_size(), $sqls, $sqlc);

		return $pdbd;
	}


	public function process(DB_Connection $dbc, $page = NULL) {
		$vm = $this->get_vm();

		$sql = $this->get_sqls($vm);

		$this->paginate_db_data->set_sql_select($sql['sqls']);
		$this->paginate_db_data->set_sql_count($sql['sqlc']);

		parent::process($dbc, $page);
	}


	private function get_sqls($vm) {
		$filename = $vm->get_page_var('ci_attachment_select', 'filename');
		$note = $vm->get_page_var('ci_attachment_select', 'note');

		$sqls = TCMS_CI_Attachment_DBC::get_sql_for_cas_grid($this->ci_id, $filename, $note);
		$sqlc = TCMS_CI_Attachment_DBC::get_sql_count_for_cas_grid($filename, $note);

		$sql['sqls'] = $sqls;
		$sql['sqlc'] = $sqlc;

		return $sql;
	}
}